//
//  EssentialViewController.m
//  Menu
//
//  Created by dllo on 15/7/8.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "EssentialViewController.h"

@interface EssentialViewController ()

@end

@implementation EssentialViewController

- (void)dealloc
{
    [_essentialTableView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"厨房必备";
    self.navigationItem.backBarButtonItem.tintColor = [UIColor blackColor];
    
    self.ingredientsArray = [NSMutableArray array];
    self.utensilsArray = [NSMutableArray array];
    
    self.essentialTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, MyWidth, MyHeight - 64-49)];
    self.essentialTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.essentialTableView];
    
    self.essentialTableView.delegate = self;
    self.essentialTableView.dataSource = self;
    
    self.backImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.backImageView.image = [UIImage imageNamed:@"essentialBG.png"];
    [self.view addSubview:self.backImageView];
    [_backImageView release];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading...";
    [self.HUD show:YES];
    
    [self loadData];
    
    [_essentialTableView release];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    
}

-(void)loadData
{
    NSString *urlStr = @"http://content.kitchenstories.de/v2/zh/content.json";
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:urlStr resultBlock:^(id result) {
        NSMutableDictionary *dataDic = [result objectForKey:@"data"];
        NSMutableArray *essentialArray = [dataDic objectForKey:@"kitchenEssentials"];
        for (NSMutableDictionary *dict in essentialArray)
        {
            EssentialModel *essential = [[EssentialModel alloc] initWithDic:dict];
            if ([essential.type isEqualToString:@"ingredients"])
            {
                [self.ingredientsArray addObject:essential];
                [essential release];
            }
            if ([essential.type isEqualToString:@"utensils"])
            {
                [self.utensilsArray addObject:essential];
                [essential release];
            }
        }
        self.bigArray = [NSMutableArray arrayWithObjects:self.ingredientsArray,self.utensilsArray, nil];
        [self.essentialTableView reloadData];
        [self.backImageView removeFromSuperview];
        [self.HUD hide:YES];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.bigArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return self.ingredientsArray.count;
    }
    if (section == 1)
    {
        return self.utensilsArray.count;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50*HEIGHT;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return @"食材";
    }
    if (section == 1)
    {
        return @"厨房用具";
    }
    return nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *essential = @"esCell";
    EssentialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:essential];
    if (cell == nil)
    {
        cell = [[EssentialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:essential];
    }
    
    if (indexPath.section == 0)
    {
        cell.essentialModel = [self.ingredientsArray objectAtIndex:indexPath.row];
    }
    if (indexPath.section == 1)
    {
        cell.essentialModel = [self.utensilsArray objectAtIndex:indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EssentialModel *essential = [[EssentialModel alloc] init];
    if (indexPath.section == 0)
    {
        essential = [self.ingredientsArray objectAtIndex:indexPath.row];
        EssentialDetailViewController *essentialDVC = [[EssentialDetailViewController alloc] init];
        essentialDVC.navigationItem.title = essential.title;
        essentialDVC.essentialDetail = essential.desc;
        essentialDVC.imgUrl = essential.image;
        [self.navigationController pushViewController:essentialDVC animated:YES];
        [essentialDVC release];
    }
    if (indexPath.section == 1)
    {
        essential = [self.utensilsArray objectAtIndex:indexPath.row];
        EssentialDetailViewController *essentialDVC = [[EssentialDetailViewController alloc] init];
        essentialDVC.navigationItem.title = essential.title;
        essentialDVC.essentialDetail = essential.desc;
        essentialDVC.imgUrl = essential.image;
        [self.navigationController pushViewController:essentialDVC animated:YES];
        [essentialDVC release];
    }
}
-(void)leftBarAction
{
    [self.navigationController popViewControllerAnimated:YES];
}





















- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
